<?php

namespace App\Modules\Blog\Api\Controllers;

use Illuminate\Http\Request;
use App\Modules\Blog\Models\Cate;
use App\Modules\Blog\Http\Resources\CateResource;
use App\Modules\Blog\Http\Requests\CateCreateRequest;
use App\Modules\Blog\Http\Requests\CateUpdateRequest;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Encore\Admin\Form;

class CateController extends Controller
{

    /**
     * current Module
     */
    protected $module = "blog";
    protected $modelclass = Cate::class;


    public function index(Request $request)
    {
        $per_page = $request->get('per_page', 10);
        $page_type = (int) $request->get('page_type', 0);

        $data = (new Cate())->AllQuery();

        if ($page_type === 2) {
            return json_ok(CateResource::collection($data));
        } elseif ($page_type) {
            $data = $data->paginate($per_page);
        } else {
            $data = $data->simplePaginate($per_page);
        }
        $data = $data->appends($request->query());
        return CateResource::collection($data)->additional([
            'status'    => '1',
            'code'      => '200',
            'url'       => $request->getRequestUri(),
        ]);
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $data = request()->all();
        json_ok($data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \App\Modules\Blog\Http\Requests\CateCreateRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->model = $model = new Cate();
        $rule = $model->getCreateRules();
        $message = $model->getMessages();
        $custom = $model->getComments();
        $data = $request->all();
        $validator = Validator::make(
            $data,
            $rule,
            $message,
            $custom
        );

        if ($validator->fails()) {
            return json_error($validator->errors()->first());
        }
        $fields = $model->getFormCreate();
        foreach ($data as $key => $value) {
            if (in_array($key, $fields)) {
                $this->form()->input($key, $value);
            }
        }

        //忽略掉不需要保存或直接使用默认值的字段
        $this->form()->ignore($model->getIgnoreFields());
        return $this->form()->store();

        /*
        DB::beginTransaction();
        try {
            $fields = $model->getTableFields();
            foreach ($data as $key => $value) {
                if (in_array($key, $fields)) $model->setAttribute($key, $value);
            }
            $result = $model->save();
            // $this->updateRelation($this->relations); 关系数据修理
            //
            DB::commit();
            return json_ok($model);
        } catch (\Exception $e) {
            DB::rollBack();
            return json_error('创建记录失败');
        }*/
    }

    public function form()
    {
        $comments = $this->model->getComments();
        $form = new Form($this->model);

        return $form;
    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $model = new Cate();
        $data = $model->findOrFail($id);
        $result['data'] =  (new CateResource($data))->resolve();
        $result['module'] = get_module($this->module);
        return json_ok($result);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \App\Modules\Blog\Http\Requests\CateUpdateRequest  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(CateUpdateRequest $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $model = new Cate();
        try {
            $model->delete();
            return json_ok("记录{$id}删除成功");
        } catch (\Exception $e) {
            return json_error($e->getMessage());
        }
    }
}
